<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统中心</el-breadcrumb-item>
            <el-breadcrumb-item>学生管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">

            <el-button  type="primary" @click="dialogVisible = true">添加学生</el-button>

            <el-table
                    :data="studentList"
                    style="width: 100%">
                <el-table-column
                        type="index"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="userName"
                        label="登录账号"
                        width="160">
                </el-table-column>
                <el-table-column
                        prop="mobile"
                        label="联系电话"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="nickName"
                        label="昵称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="学生状态"
                        width="180">
                    <template slot-scope="scope">
                        {{scope.row.status|statusFit}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="studentFee"
                        label="学生余额"
                        width="180">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="320">
                    <!--点击按钮 需要绑定@click的时间 例如 @click="add"-->
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="editById(scope.row.id)">修改</el-button>
                        <el-button type="primary" size="mini" @click="delById(scope.row.id)">删除</el-button>
                        <el-button type="primary" size="mini" @click="openEditStudentPass(scope.row.id)">修改学生密码</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="page.pageCurrent"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="page.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
        </el-card>
        <el-dialog
                title="添加学生"
                :visible.sync="dialogVisible"
                width="30%">
            <el-form ref="form" :model="studentForm" label-width="80px" >
                <el-form-item label="登录账号">
                    <el-input v-model="studentForm.userName"></el-input>
                </el-form-item>
                <el-form-item label="学生名称">
                    <el-input v-model="studentForm.nickName"></el-input>
                </el-form-item>
                <el-form-item label="学生电话">
                    <el-input v-model="studentForm.mobile"></el-input>
                </el-form-item>
                <el-form-item label="学生余额">
                    <el-input v-model="studentForm.studentFee"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearStudent">取 消</el-button>
                 <el-button type="primary" @click="addStudent">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="编辑学生"
                :visible.sync="dialogVisible1"
                width="30%">
            <el-form ref="form" :model="studentForm" label-width="80px" >
                <el-form-item label="登录账号">
                    <el-input v-model="studentForm.userName"></el-input>
                </el-form-item>
                <el-form-item label="学生名称">
                    <el-input v-model="studentForm.nickName"></el-input>
                </el-form-item>
                <el-form-item label="学生电话">
                    <el-input v-model="studentForm.mobile"></el-input>
                </el-form-item>
                <el-form-item label="学生余额">
                    <el-input v-model="studentForm.studentFee"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearStudent">取 消</el-button>
                 <el-button type="primary" @click="updateStudent">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="修改学生密码"
                :visible.sync="dialogVisible2"
                width="30%">
            <el-form ref="form" :model="studentForm" label-width="100px" >
                <el-form-item label="学生新密码">
                    <el-input v-model="studentForm.studentPass"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearStudent">取 消</el-button>
                 <el-button type="primary" @click="editStudentPass">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "AdminList",
        data(){
            return{
                studentList:[],
                student:{},
                studentForm:{},
                dialogVisible:false,
                dialogVisible1:false,
                dialogVisible2:false,
                studentid:null,
                page:{
                    pageCurrent:0,
                    pageSize:5
                },
                total:0,
            }
        },
        methods:{
            //实现分页
            //每页记录数
            handleSizeChange:function(val){
                this.page.pageSize = val
                this.studentSel()
            },
            //当前页
            handleCurrentChange:function(val){
                this.page.pageCurrent = val
                this.studentSel()
            },

            //列表实现
            studentSel:function () {
                this.$http.post('/admin/getStudentByPage?pageCurrent='+this.page.pageCurrent+
                        '&pageSize='+this.page.pageSize).then((res)=>{
                    this.studentList = res.data.result.pages.records
                    this.total =  res.data.result.pages.total;
                })
            },

            //逻辑删除
            delById:function (id) {
                this.$http.post('/admin/delStudentUser?studentId='+id).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    this.studentSel();
                })
            },

            //添加学生
            addStudent:function () {
                this.$http.post('admin/addStudentUser?userName='+this.studentForm.userName+
                    '&nickName='+this.studentForm.nickName+
                    '&mobile='+this.studentForm.mobile+
                    '&fee='+this.studentForm.studentFee).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearStudent();
                })
            },

            //修改回显
            editById:function(id){
                this.dialogVisible1=true
                this.$http.post('/admin/getStudentInfo?userId='+id).then((res)=>{
                    if (res.data.success) {
                       this.studentForm = res.data.result.student
                    }else{
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
            },

            //修改学生
            updateStudent:function(id){
                this.$http.post('student/setStudentInfo?userId='+this.studentForm.id+
                    '&userName='+this.studentForm.userName+
                    '&nickName='+this.studentForm.nickName+
                    '&mobile='+this.studentForm.mobile+
                    '&fee='+this.studentForm.studentFee).then((res)=>{
                        if (res.data.success) {
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: res.data.message,
                                type: 'error'
                            });
                        }
                        this.clearStudent();
                })
            },

            //打开修改密码的dialog
            openEditStudentPass:function(id){
                this.studentForm.id=id;
                this.dialogVisible2=true;

            },

            //修改密码
            editStudentPass:function(id){
                this.dialogVisible2=false
                this.$http.post('/student/setStudentPass?studentId='+this.studentForm.id+
                    '&studentPass='+this.studentForm.studentPass).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    this.clearStudent()
                })
            },

            //清空表单
            clearStudent:function(){
                this.dialogVisible=false
                this.dialogVisible1=false
                this.dialogVisible2=false
                this.studentForm.id=null
                this.studentForm.studentPass=""
                this.studentForm.userName=""
                this.studentForm.mobile=""
                this.studentForm.nickName=""
                this.studentForm.studentFee=""
                this.studentSel()
            }

        },
        filters:{
            statusFit:function (val) {
                if (val == '0'){
                    return "在读"

                }
                if (val == '1'){
                    return "锁定"
                }
            }
        },
        mounted() {
            this.studentSel();
        }

    }
</script>

<style scoped>


</style>
